034-find-first-and-last-position-of-element-in-sorted-array.py
problem: ---
problem:

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].

Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
---

-----------------------------------------------------------------------
bug_fixes: ---
bug_fixes:
Replace `self.binarySearch(nums, target, False)` with `[leftIndex,self.binarySearch(nums, target, False)]` on line 6.
Replace `high if flag else low` with `low if flag else high` on line 16.
---

-----------------------------------------------------------------------
bug_desc: ---
bug_desc:
On line 6, only the result of the binary search is being returned. Both the leftIndex and the search result need to be returned, formatted in a list.
On line 15, the binarySearch function should return low when flag is True (finding the leftmost occurrence), and high when flag is False (finding the rightmost occurrence). However, the return statement is reversed.
---

-----------------------------------------------------------------------
line_no: ---
line_no:
6
---

-----------------------------------------------------------------------
buggy_code: ---
buggy_code:
1. class Solution(object):
2.     def searchRange(self, nums, target):
3.         leftIndex = self.binarySearch(nums, target, True)
4.         if leftIndex == len(nums) or nums[leftIndex] != target:
5.             return [-1,-1]
6.         return self.binarySearch(nums, target, False)
7.         
8.     def binarySearch(self, nums, target, flag):
9.         low, high = 0, len(nums) - 1
10.         while low <= high:
11.             mid = (low+high)//2
12.             if nums[mid] > target or (nums[mid] == target and flag):
13.                 high = mid - 1
14.             else:
15.                 low = mid + 1
16.         return high if flag else low
---

-----------------------------------------------------------------------
correct_code: ---
correct_code:
1. class Solution(object):
2.     def searchRange(self, nums, target):
3.         leftIndex = self.binarySearch(nums, target, True)
4.         if leftIndex == len(nums) or nums[leftIndex] != target:
5.             return [-1,-1]
6.         return [leftIndex,self.binarySearch(nums, target, False)]
7.         
8.     def binarySearch(self, nums, target, flag):
9.         low, high = 0, len(nums) - 1
10.         while low <= high:
11.             mid = (low+high)//2
12.             if nums[mid] > target or (nums[mid] == target and flag):
13.                 high = mid - 1
14.             else:
15.                 low = mid + 1
16.         return low if flag else high
---

-----------------------------------------------------------------------
